package com.alan;

import cn.hutool.core.date.DateUtil;
import com.loser.redis.core.RedisHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import redis.clients.jedis.Response;

import java.util.List;

@SpringBootConfiguration
@SpringBootApplication
public class StarterApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(StarterApplication.class, args);
        RedisHandler redisHandler = context.getBean(RedisHandler.class);

        // 01 测试 spring boot 的自定注入
        System.out.println("======================================================");
        String key = "loser:test:key";
        System.out.println("01 redisHandler.exists(key) = " + redisHandler.exists(key));
        System.out.println();
        System.out.println("02 redisHandler.del(key) = " + redisHandler.del(key));
        System.out.println();
        System.out.println("03 redisHandler.set(key, DateUtil.now(), 10000) = " + redisHandler.set(key, DateUtil.now(), 10000));
        System.out.println();
        System.out.println("04 redisHandler.get(key) = " + (redisHandler.get(key)));
        System.out.println("======================================================");


        // 02 调用 管道 示例
        Response<List<Object>> pipelineResult = redisHandler.executePipeline((pipeline) -> {
            pipeline.multi();
            // 多步操作
            pipeline.set("key", DateUtil.now());
            pipeline.get("key");
            return pipeline.exec();
        });
        for (Object pRes : pipelineResult.get()) {
            System.out.println(pRes);
        }

    }

}
